//
//  MyViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/19.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "MyViewController.h"
#import "MyHeaderView.h"
#import "WishListViewController.h"
#import "CollectViewController.h"
#import "PlaytourViewController.h"
#import "MemberViewController.h"
#import "UserModel.h"
#import "WishNumberViewController.h"
#import "AboutWeViewController.h"
#import "SuggestViewController.h"
#import "SetupViewController.h"
#import "TestViewController.h"
#import "ResponseManager.h"
#import "ResponseManager+UserInfo.h"
#import <UIImageView+WebCache.h>
#import "FBProgressView.h"
static NSString *kMyindetifierCell = @"MyCell";

@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,MyHeaderViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic ,strong)NSArray *dataSoucre;
@property (nonatomic ,strong)MyHeaderView *headerView;


@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupSubView];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
//    UserModel *model = [UserModel sharedUserModel];
    
//    if (model.islogin == YES) {
//        _headerView.lable.hidden = YES;
//    }else {
//        _headerView.lable.hidden = NO;
//        
//    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [userDefaults objectForKey:@"userInfo"];
    NSLog(@"%@",dic);
    UserModel *model = [UserModel sharedUserModel];
    if ( model.islogin == YES) {
         [FBProgressView showWithMessage:nil];
       [ResponseManager getUserInfoUserId:dic[@"id"] completionHandler:^(FBResponse *response) {
            [FBProgressView hide];
           NSString *urlString = [NSString stringWithFormat:@"%@%@",@"http://www.scxxhs.com/huzhu",response.resultValue[@"avatar"]];
           NSURL *url = [NSURL URLWithString:urlString];
          
           if (response.success) {
            
               [self.headerView.headerImage sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                   
               }];
               
               
               self.headerView.lable.text = response.resultValue[@"user_nicename"];

                    
              
           }else {
               NSLog(@"%@",response.resultDesc);
           }
           
       }];
    }
    if (model.islogin == NO) {
        
        self.headerView.headerImage.image = [UIImage imageNamed:@"个人中心-默认头像"];
        self.headerView.lable.text = @"点击登录注册";
        
    }
    

}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBarHidden = NO;
}

- (void)setupSubView {
     self.headerView = [MyHeaderView loadMyHeaderView];
    _headerView.frame = CGRectMake(0, 0, kScreenWidth, 300 * kScreenHeightScale);
    _headerView.delegate = self;
    self.tableView.tableHeaderView = _headerView;
    
   
}

- (void)myHeaderView:(MyHeaderView *)myHeaderView didTapedCategoryButtonAtIndex:(NSInteger)index {
    UserModel *model = [UserModel sharedUserModel];
    MemberViewController *memberVC = [[MemberViewController alloc]init];

    if (index == 0) {
        if (model.islogin == YES) {
            WishListViewController *wishVC = [[WishListViewController alloc]init];
            [self.navigationController pushViewController:wishVC animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];
        }
        
    }
    if (index == 1) {
        if (model.islogin == YES) {
            PlaytourViewController *playtourVC = [[PlaytourViewController alloc]init];
            [self.navigationController pushViewController:playtourVC animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];

        }
      
    }
    
    if (index == 2) {
        if (model.islogin == YES) {
            CollectViewController *collectVC = [[CollectViewController alloc]init];
            [self.navigationController pushViewController:collectVC animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];

        }
     
    }
    if (index == 3) {
        UserModel *model = [UserModel sharedUserModel];
        if (model.islogin == NO) {
            MemberViewController *memberVC = [[MemberViewController alloc]init];
            [self.navigationController pushViewController:memberVC animated:YES];
        }else {
            
            UIImagePickerController *imagePickVC = [[UIImagePickerController alloc] init];
            // 设置代理对象
            imagePickVC.delegate = self;
            // 开启编辑模式
            imagePickVC.allowsEditing = YES;
            // 设置媒体类型
            imagePickVC.mediaTypes = @[@"public.image"];
            
            UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"请选择" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *actionCamera=[UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                imagePickVC.sourceType=UIImagePickerControllerSourceTypeCamera;
                [self presentViewController:imagePickVC animated:YES completion:nil];
                
            }];
            
            UIAlertAction *actionPhotoLIbrary=[UIAlertAction actionWithTitle:@"打开相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                imagePickVC.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
                [self presentViewController:imagePickVC animated:YES completion:nil];
                
            }];
            
          
            
            UIAlertAction *cancelAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            
            [alertController addAction:actionCamera];
            
            
            [alertController addAction:actionPhotoLIbrary];
            
            [alertController addAction:cancelAction];
            
            [self presentViewController:alertController animated:YES completion:nil];
            
        }
        
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
    [self.headerView.headerImage setImage:image];
    
 NSData *imageData = UIImageJPEGRepresentation(image, 1);
//    NSString *tempFilePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
//    NSSearchPathForDirectoriesInDomains(<#NSSearchPathDirectory directory#>, NSSearchPathDomainMask domainMask, <#BOOL expandTilde#>)
    NSString *tmpPath = NSTemporaryDirectory();
    NSString *imagePath = [tmpPath stringByAppendingPathComponent:@"header.jpg"];
    [imageData writeToFile:imagePath atomically:YES];
    
    [ResponseManager startUploadRequestWithURLString:kUpdateAvatarURL parameters:nil filePathList:@[imagePath] fileKeyList:@[@"avatar"] completionHandler:^(FBResponse *response) {
        
      
        
    }];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 2;
    }
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return kScreenHeightScale * 13;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = RGBCOLOR(239.0f, 239.0f, 244.0f, 1);
    
    return view;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UserModel *model = [UserModel sharedUserModel];
    MemberViewController *memberVC = [[MemberViewController alloc]init];
    if (indexPath.section == 0 && indexPath.row == 0) {
        if (model.islogin == YES) {
            WishNumberViewController *wishNumberVC = [[WishNumberViewController alloc]init];
            [self.navigationController pushViewController:wishNumberVC animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];

        }
     
    }
    if (indexPath.section == 0 && indexPath.row == 1) {
        if (model.islogin == YES) {
            TestViewController *purseVC= [[TestViewController alloc] init];
//            PurseViewController *purseVC = [[PurseViewController alloc]init];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.navigationController pushViewController:purseVC animated:YES];

            });
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];
        }
    }
    if (indexPath.section == 1 && indexPath.row == 0) {
        AboutWeViewController *aboutVC = [[AboutWeViewController alloc]init];
        [self.navigationController pushViewController:aboutVC animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        if (model.islogin == YES) {
            SuggestViewController *suggest = [[SuggestViewController alloc]init];
            
            [self.navigationController pushViewController:suggest animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];
        }
    }
    if (indexPath.section == 1 && indexPath.row == 2) {
        if (model.islogin == YES) {
            SetupViewController *setupVC = [[SetupViewController alloc]init];
            NSLog(@"%@",self.headerView.lable.text);
           setupVC.name = self.headerView.lable.text;
            [self.navigationController pushViewController:setupVC animated:YES];
        }else {
            [self.navigationController pushViewController:memberVC animated:YES];
        }
        
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMyindetifierCell];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyindetifierCell];
    }
    
    if (indexPath.section == 0 ) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"愿望次数";
            cell.imageView.image = [UIImage imageNamed:@"个人中心-愿望次数"];
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"钱包";
            cell.imageView.image = [UIImage imageNamed:@"个人中心-钱包"];
        }
        
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"关于我们";
            cell.imageView.image = [UIImage imageNamed:@"个人中心-关于我们"];
            
        }
        if (indexPath.row == 1) {
            cell.textLabel.text = @"建议反馈";
            cell.imageView.image = [UIImage imageNamed:@"个人中心-建议反馈"];
        }
        if (indexPath.row == 2) {
            cell.textLabel.text = @"设置";
            cell.imageView.image = [UIImage imageNamed:@"个人中心-设置"];
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    
    
    return cell;
    
}


- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = RGBCOLOR(239.0f, 239.0f, 244.0f, 1);
    }
    return _tableView;
}





@end
